package com.gexin.controller.system.notice;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.StringUtil;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.notice.*;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.JsonKit;
import com.jfinal.weixin.sdk.msg.out.News;

import java.util.List;

/**
 * @author 黎峻
 * @Title: 通知管理 里面包含了资讯和公告
 * @date 2019/9/2615:39
 */
@ControllerBind(controllerKey = "/system/notice", viewPath = "WEB-INF/system/notice/")
public class NoticeController extends SystemBaseController {

	/**
	 * ================== 公告类型管理 ==================
	 */
	public void type(){
		render("notice_type_list.html");
	}

	public void loadTypeData(){
		NoticeType type =new NoticeType();
		type.setNoticeType(getParaToInt("type_id"));
		type.setState(getParaToInt("state"));
		type.setTypeName(getPara("title"));
		success(NoticeType.dao.query(getpage(),getpageSize(),type));
	}

	public void saveType(){
		NoticeType type= getModel(NoticeType.class,"t");
		if(type.getState()==null){
			type.setState(1);
		}
		if(type.getId()==null){
			type.save();
			success("添加成功");
			return;
		}else{
			type.update();
			success("修改成功");
			return;
		}
	}

	/**
	 * 删除类型
	 */
	public void deleteType(){
		String id = getPara();
		if(StringUtil.isBlank(id)){
			fail("id不能为空!");
			return;
		}
		NoticeType noticeType = NoticeType.dao.findById(id);
		if(noticeType==null){
			fail("当前类型不存在!");
			return;
		}

		List<NoticeNews> noticeNews = NoticeNews.dao.find("select * from t_notice_type where type_id=?" , id);
		List<NoticePlacard> placardList = NoticePlacard.dao.find("select * from t_notice_placard where type_id=?" , id);
		if((noticeNews!=null&&noticeNews.size()!=0) || (placardList!=null&&placardList.size()!=0)){
			fail("当前类型已有关联!删除失败!");
			return;
		}
		boolean delete = noticeType.delete();
		renderJson(new ApiResponse(delete,delete?"删除成功":"删除失败"));
	}

	/**
	 * ================== 资讯管理 ==================
	 */
	public void news(){
		setAttr("type_list", JsonKit.toJson(NoticeType.dao.findByNoticeType(1)));
		render("news_list.html");
	}

	public void loadNewsDate(){
		NoticeNews news =new NoticeNews();
		news.setState(getParaToInt("state"));
		news.setTypeId(getParaToInt("type_id"));
		news.setTitle(getPara("title",null));
		success(NoticeNews.dao.query(getpage(),getpageSize(),news));
	}

	public void addNews(){
		Integer id =getParaToInt(0);
		NoticeNews news =NoticeNews.dao.findById(id);
		if(news==null || news.getId()==null){
			news= new NoticeNews();
		}
		setAttr("news",news);
		setAttr("type_list", NoticeType.dao.findByNoticeType(1));
		render("news_add.html");
	}

	public void saveNews(){
		NoticeNews news =getModel(NoticeNews.class,"news");
		if(news.getState()==null){
			news.setState(1);
		}

		if(news.getIsIndex()==null||0==news.getIsIndex()){
			news.setIsIndex(0);
		}
		if(news.getId()==null){
			news.save();
			success("添加成功");
			return;
		}else{
			news.update();
			success("修改成功");
			return;
		}
	}


	//加载资讯评论
	public void commentNews(){
		setAttr("news_id",getParaToInt(0));
		render("news_comment_list.html");
	}

	public void loadCommentNewsData(){
		NoticeNewsComment comment =new NoticeNewsComment();
		comment.setContent(getPara("title"));
		comment.setState(getParaToInt("state"));
		comment.setNewsId(getParaToInt("news_id"));
		success(NoticeNewsComment.dao.query(getpage(),getpageSize(),comment));
	}

	public void delete(){
		String id = getPara();
		if(StringUtil.isBlank(id)){
			fail("id不能为null");
			return;
		}
		boolean delete = NoticeNews.dao.deleteById(id);
		renderJson(new ApiResponse(delete,delete?"删除成功!":"删除失败!"));
	}

	public  void deleteComment(){
		String id = getPara();
		if(StringUtil.isBlank(id)){
			fail("id不能为空!");
			return;
		}
		NoticeNewsComment comment = NoticeNewsComment.dao.findById(id);
		if(comment==null){
			fail("回复不存在!");
			return;
		}
		Integer deleteNumber = 0;
		List<NoticeNewsCommentReply> noticeNewsCommentReplies = NoticeNewsCommentReply.dao.findCommentId(comment.getId());
		for (NoticeNewsCommentReply noticeNewsCommentReply : noticeNewsCommentReplies) {
			noticeNewsCommentReply.delete();
			deleteNumber += 1;
		}
		NoticeNews noticeNews = NoticeNews.dao.findById(comment.getNewsId());
		boolean delete = comment.delete();
		if(delete){
			deleteNumber += 1;
			Integer commentCount = noticeNews.getCommentCount();
			if(commentCount>=deleteNumber){
				noticeNews.setCommentCount(commentCount-deleteNumber);
				noticeNews.update();
			}
		}
		renderJson(new ApiResponse(delete,delete?"删除成功!":"删除失败!"));
	}


	/**
	 * ================== 公告管理 ==================
	 */
	public void placard(){
		setAttr("type_list", JsonKit.toJson(NoticeType.dao.findByNoticeType(2)));
		render("placard_list.html");
	}

	public void loadPlacardData(){
		NoticePlacard placard =new NoticePlacard();
		placard.setState(getParaToInt("state"));
		placard.setTypeId(getParaToInt("type_id"));
		placard.setTitle(getPara("title"));
		success(NoticePlacard.dao.query(getpage(),getpageSize(),placard));
	}

	public void  addPlacard(){
		Integer id =getParaToInt(0);
		NoticePlacard placard =NoticePlacard.dao.findById(id);
		if(placard==null || placard.getId()==null){
			placard= new NoticePlacard();
		}
		setAttr("placard",placard);
		setAttr("type_list", NoticeType.dao.findByNoticeType(2));
		render("placard_add.html");
	}

	public void deletePlacard(){
		String id = getPara();
		if(StringUtil.isBlank(id)){
			fail("id不能为空!");
			return;
		}
		NoticePlacard placard = NoticePlacard.dao.findById(id);
		if(placard==null){
			fail("资讯不存在!");
			return;
		}
		boolean delete = placard.delete();
		renderJson(new ApiResponse(delete,delete?"删除成功!":"删除失败"));
	}

	public void savePlacard(){
		NoticePlacard placard =getModel(NoticePlacard.class,"placard");
		if(placard.getState()==null){
			placard.setState(1);
		}
		if(placard.getId()==null){
			placard.save();
			success("添加成功");
			return;
		}else{
			placard.update();
			success("修改成功");
			return;
		}
	}

}
